﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using SIE.SCADA.SqlSugar;
using SysManage.User;
using Model.Core;

namespace SIE.SCADA.ZemtDemo.Common
{
    class SubmitDB
    {
        static PgSqlAccess pgsql = new PgSqlAccess();
        public static void Sumbit_SMDC_Disid(string sn, string disidNo, State state)
        {
            using (var ctx = pgsql.GetClient())
            {
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    var smdc_disid_t = ormClient.SqlSugarClient.Queryable<smdc_disid_t>().
                        Where(m => m.serial_number == sn && m.disidno == disidNo).First();
                    if (smdc_disid_t == null) return;

                    smdc_disid_t.state_int = (int)state;
                    ormClient.SqlSugarClient.Updateable(smdc_disid_t).IgnoreColumns(ignoreAllNullColumns: true)
                        .Where(m => m.id == smdc_disid_t.id).ExecuteCommand();
                }
            }
        }

        /// <summary>
        /// 更新工步记录表信息
        /// </summary>
        /// <param name="v_Scada_Jobstep"></param>
        public static void Sumbit_JobStep(v_scada_jobsteprecord v_Scada_Jobstep)
        {
            using (var ctx = pgsql.GetClient())
            {
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    var scada_jobstep = ormClient.SqlSugarClient.Queryable<scada_jobstep_record>().Where(m => m.id == v_Scada_Jobstep.id)
                        .First();
                    if (scada_jobstep == null) return;

                    scada_jobstep.state_int = (int)v_Scada_Jobstep.state_int;
                    scada_jobstep.actangle = v_Scada_Jobstep.actangle;
                    scada_jobstep.acttorque = v_Scada_Jobstep.acttorque;
                    scada_jobstep.torquestatus = v_Scada_Jobstep.torquestatus;
                    ormClient.SqlSugarClient.Updateable(scada_jobstep).Where(m=>m.id == v_Scada_Jobstep.id).
                        IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
                }
            }
        }

        /// <summary>
        /// 提交强制过站信息
        /// </summary>
        /// <param name="disidNo"></param>
        /// <param name="sn"></param>
        /// <param name="stepCode"></param>
        /// <param name="user"></param>
        public static void Submit_ByPass(string disidNo, string sn, string stepCode, string user)
        {
            using (var ctx = pgsql.GetClient())
            {
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    var scada_process_record = ormClient.SqlSugarClient.Queryable<scada_process_record>().
                        Where(m => m.serial_number == sn && m.disidno == disidNo && m.jobstep_code == stepCode).First();
                    if (scada_process_record == null) return;

                    scada_process_record.isbypass = 1;
                    scada_process_record.bypassuser = user;
                    ormClient.SqlSugarClient.Updateable(scada_process_record).Where(m => m.id == scada_process_record.id).
                        IgnoreColumns(ignoreAllNullColumns: true).
                        ExecuteCommand();
                }
            }
        }
        /// <summary>
        /// 提交信息到工序SN关联表SCADA_DisProcess
        /// </summary>
        /// <param name="Disque_Code">工序</param>
        /// <param name="sn">sn</param>
        /// <param name="disidNo">工单</param>
        /// <param name="userid">用户id</param>
        /// <param name="actualWtime">用时</param>
        public static void Submit_DisProcess(string Disque_Code, string sn, string disidNo, User user, decimal actualWtime, DateTime startTime)
        {
            using (var ctx = pgsql.GetClient())
            {
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    var disprocess = ormClient.SqlSugarClient.Queryable<scada_disprocess>().
                        Where(m => m.disidno == disidNo && m.serial_number_str == sn && m.processcode_str == Disque_Code).First();
                    if (disprocess == null) return;

                    disprocess.state_int = (int)State.Finish;
                    disprocess.endtime_dt = DateTime.Now;
                    disprocess.userid_str = user?.UserID;
                    disprocess.username_str = user?.UserName;
                    disprocess.starttime_dt = startTime;
                    disprocess.actualworkhour_num = actualWtime;
                    ormClient.SqlSugarClient.Updateable<scada_disprocess>(disprocess).IgnoreColumns(ignoreAllNullColumns: true).
                       Where(m => m.id == disprocess.id)
                           .ExecuteCommand();
                }
            }
        }

        /// <summary>
        /// 提交信息到工序记录SN关联表
        /// </summary>
        /// <param name="Disque_Code">工序</param>
        /// <param name="sn">sn</param>
        /// <param name="disidNo">工单</param>
        /// <param name="userid">用户id</param>
        /// <param name="actualWtime">用时</param>
        public static void Submit_ProcessRecrd(string Disque_Code, string sn, string disidNo, User user, decimal actualWtime, DateTime startTime)
        {
            using (var ctx = pgsql.GetClient())
            {
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    var scada_process_record = ormClient.SqlSugarClient.Queryable<scada_process_record>().
                        Where(m => m.serial_number == sn && m.disidno == disidNo && m.disque_code == Disque_Code).First();
                    if (scada_process_record == null) return;

                    scada_process_record.state_int = (int)State.Finish;
                    scada_process_record.endtime_dt = DateTime.Now;
                    scada_process_record.opertor = user?.UserID;
                    scada_process_record.starttime_dt = startTime;
                    scada_process_record.aworkhour_num = actualWtime;
                    ormClient.SqlSugarClient.Updateable(scada_process_record).IgnoreColumns(ignoreAllNullColumns: true).
                        Where(m => m.id == scada_process_record.id).ExecuteCommand();
                }
            }
        }

        /// <summary>
        /// KJF 20210318 新增
        /// 工单状态更新
        /// </summary>
        /// <param name=""></param>     
        public static bool TryUpdateWorkStateToFinish( string workNo,string serialNumber, string processNo )
        {
            using (var ctx = pgsql.GetClient())
            {
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    var scada_process_record = ormClient.SqlSugarClient.Queryable<scada_process_record>().
                        Where(m =>m.disidno == workNo & m.serial_number == serialNumber && m.state_int != (int)State.Finish
                         && m.op_group == processNo).First();
                    if (scada_process_record == null)
                    {
                        var WorkInfo = ormClient.SqlSugarClient.Queryable<smdc_disid_t>().
                        Where(m => m.serial_number == serialNumber && m.disque == processNo).First(); ;
                        if (WorkInfo != null) {
                            WorkInfo.state_int = (int)State.Finish;
                            ormClient.SqlSugarClient.Updateable(WorkInfo).IgnoreColumns(ignoreAllNullColumns: true).
                            Where(m => m.id == WorkInfo.id).ExecuteCommand();
                            return true;
                        }
                    }
                }
                return false;
            }
        }

        /// <summary>
        /// KJF 20210410 新增
        /// 工单状态更新
        /// </summary>
        /// <param name=""></param>     
        public static bool TryUpdateWorkStateToFinishById(int id,string userid)
        {
            using (var ctx = pgsql.GetClient())
            {
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    var scada_process_record = ormClient.SqlSugarClient.Queryable<scada_process_record>().
                        Where(m => m.woid == id && m.state_int != (int)State.Finish).First();
                    if (scada_process_record == null)
                    {
                        var WorkInfo = ormClient.SqlSugarClient.Queryable<smdc_disid_t>().
                        Where(m =>  m.id == id).First(); 
                        if (WorkInfo != null)
                        {
                            WorkInfo.state_int = (int)State.Finish;
                            WorkInfo.operatorcode = userid;
                            WorkInfo.modifytime_dt = DateTime.Now;
                            ormClient.SqlSugarClient.Updateable(WorkInfo).IgnoreColumns(ignoreAllNullColumns: true).
                            Where(m => m.id == WorkInfo.id).ExecuteCommand();
                            return true;
                        }
                    }
                }
                return false;
            }
        }

        /// <summary>
        /// KJF 20210519 新增
        /// 工件状态更新
        /// </summary>
        /// <param name=""></param>     
        public static bool TryUpdateProdStateToFinishById(int id, string userid)
        {
            using (var ctx = pgsql.GetClient())
            {
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    var scada_process_record = ormClient.SqlSugarClient.Queryable<scada_process_record>().
                        Where(m => m.woid == id && m.state_int != (int)State.Finish).First();
                    if (scada_process_record == null)
                    {
                        var WorkInfo = ormClient.SqlSugarClient.Queryable<scada_product_info>().
                        Where(m => m.id == id).First();
                        if (WorkInfo != null)
                        {
                            WorkInfo.state_int = (int)State.Finish;
                            WorkInfo.operatorcode = userid;
                            WorkInfo.modifytime_dt = DateTime.Now;
                            ormClient.SqlSugarClient.Updateable(WorkInfo).IgnoreColumns(ignoreAllNullColumns: true).
                            Where(m => m.id == WorkInfo.id).ExecuteCommand();
                            return true;
                        }
                    }
                }
                return false;
            }
        }


        public static void Submit_ProcessRecrd(scada_process_record scada_Process_Record)
        {
            using (var ctx = pgsql.GetClient())
            {
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    ormClient.SqlSugarClient.Updateable(scada_Process_Record).IgnoreColumns(ignoreAllNullColumns: true).
                        Where(m => m.id == scada_Process_Record.id).ExecuteCommand();
                }
            }
        }

        /// <summary>
        /// 提交信息到工序SN关联表
        /// </summary>
        /// <param name="Disque_Code">工序</param>
        /// <param name="sn">sn</param>
        /// <param name="disidNo">工单</param>
        /// <param name="userid">用户id</param>
        public static void Submit_LastProcess(string Disque_Code, string sn, string disidNo, User user)
        {
            using (var ctx = pgsql.GetClient())
            {
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    var scada_disprocess = ormClient.SqlSugarClient.Queryable<scada_disprocess>().
                        Where(m => m.serial_number_str == sn && m.disidno == disidNo && m.processcode_str == Disque_Code)
                        .First();

                    if (scada_disprocess == null) return;
                    scada_disprocess.checkuserid_str = user?.UserID;
                    scada_disprocess.checkusername_str = user?.UserName;
                    ormClient.SqlSugarClient.Updateable<scada_disprocess>(scada_disprocess).IgnoreColumns(ignoreAllNullColumns: true).
                        Where(m => m.id == scada_disprocess.id)
                        .ExecuteCommand();
                }
            }
        }

        /// <summary>
        /// 提交数据到工单表
        /// </summary>
        /// <param name="sn"></param>
        public static void Submit_WO(string sn,string disidNo)
        {
            using (var ctx = pgsql.GetClient())
            {
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    var smdc_disid_t = ormClient.SqlSugarClient.Queryable<smdc_disid_t>().
                        Where(m => m.serial_number == sn && m.disidno == disidNo).First();
                    if (smdc_disid_t == null) return;
                    smdc_disid_t.state_int = (int)State.Finish;
                    smdc_disid_t.modifytime_dt = DateTime.Now;
                    ormClient.SqlSugarClient.Updateable<smdc_disid_t>(smdc_disid_t).Where(m => m.id == smdc_disid_t.id)
                        .ExecuteCommand();
                       
                }
            }
        }

        /// <summary>
        /// 提交数据到工件表
        /// </summary>
        /// <param name="sn"></param>
        public static void Submit_Prod(scada_product_info prod )
        {
            using (var ctx = pgsql.GetClient())
            {
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {

                    ormClient.SqlSugarClient.Updateable<scada_product_info>(prod).Where(m => m.id == prod.id)
                        .ExecuteCommand();

                }
            }
        }


        /// <summary>
        /// 插入sn工序关联表
        /// </summary>
        /// <param name="sn"></param>
        /// <param name="process">工序</param>
        public static void InsertDisProcess(string sn, List<scada_process> process, string disidNo)
        {
            var group = process.GroupBy(m => m.disque_code).Select(g => new scada_process()
            {
                disque_code = g.Key,
                disque_name = g.Select(m => m.disque_name).FirstOrDefault() ?? "",
                disque_qty = g.Select(m => m.disque_qty).FirstOrDefault() ?? 1,
                matid = g.Select(m => m.matid).FirstOrDefault() ?? "",
                mbom = g.Select(m => m.mbom).FirstOrDefault() ?? "",
                matdesc = g.Select(m => m.matdesc).FirstOrDefault() ?? "",
                techprocess = g.Select(m => m.techprocess).FirstOrDefault() ?? "",
                small_version = g.Select(m => m.small_version).FirstOrDefault() ?? "",
                workhour_num = g.Select(m => m.workhour_num).FirstOrDefault(),
            }).ToList();
            foreach (var item in group)
            {
                var disProcess = new scada_disprocess()
                {
                    serial_number_str = sn,
                    matid = item.matid,
                    mbom = item.mbom,
                    processcode_str = item.disque_code.Trim(),
                    processname_str = item.disque_name.Trim(),
                    processno_int = Convert.ToInt32(item.disque_qty),
                    // ProcessNO_Int = Convert.ToInt32(item.Disque_Code.Trim().Substring(0,1)),
                    state_int = (int)State.Wait,
                    matdesc = item.matdesc,
                    techprocess = item.techprocess,
                    small_version = item.small_version,
                    disidno = disidNo,
                    workhour_num = item.workhour_num,
                };
                using (var ctx = pgsql.GetClient())
                {
                    if (pgsql.DBClient is SmdcOrmClient ormClient)
                    {
                        ormClient.SqlSugarClient.Insertable<scada_disprocess>(disProcess).IgnoreColumns(it => it == "id").ExecuteCommand();
                    }
                }
             }
        }

        /// <summary>
        /// 插入工序历史表
        /// </summary>
        /// <param name="sCADA_Processes">工序集合</param>
        /// <param name="sn">SN</param>
        /// <param name="disidNo">工单号</param>
        public static void InsertProcessRecord(List<scada_process> sCADA_Processes,smdc_disid_t workInfo )
        {
            var smallVersion = sCADA_Processes.FirstOrDefault()?.small_version;
            using (var ctx = pgsql.GetClient())
            {
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    //var exsits = ormClient.SqlSugarClient.Queryable<scada_process_record>().
                    //    Where(m => m.serial_number == sn && m.disidno == disidNo && m.small_version == smallVersion).First();
                    //if (exsits != null) return;
                    List<scada_process_record> Process_Records = new List<scada_process_record>();
                    foreach (var item in sCADA_Processes)
                    {
                        var CreateUser_Str = Env.DefaultUserName?.Value;
                        var process_Record = new scada_process_record()
                        {
                            disque_code = item.disque_code,
                            disque_name = item.disque_name,
                            disque_qty = item.disque_qty,
                            jobstep = item.jobstep,
                            jobstep_code = item.jobstep_code,
                            matdesc = item.matdesc,
                            matid = item.matid,
                            mbom = item.mbom,
                            small_version = item.small_version,
                            techprocess = item.techprocess,
                            
                            createuser_str = CreateUser_Str,
                            workhour_num = item.workhour_num,
                            
                            createtime_dt = System.DateTime.Now,
                            torquetimes = item.torquetimes,
                            op_group = item.op_group,
                            equipmentcode = item.equipmentCode,

                            serial_number = workInfo.serial_number,
                            disidno = workInfo.disidno,
                            woid = workInfo.id
                            
                        };

                        //try
                        //{
                            var res = ormClient.SqlSugarClient.Insertable<scada_process_record>(process_Record)
                                .IgnoreColumns(ignoreNullColumn: true)
                                .IgnoreColumns(it => it == "id").ExecuteReturnEntity();
                        //}
                        //catch (Exception ex)
                        //{
                        //    System.Diagnostics.Trace.WriteLine($"保存工序出错:{ex.Message}");
                        //    logger
                        //}
                    }
                }
            }
        }


        /// <summary>
        /// 插入工序历史表
        /// </summary>
        /// <param name="sCADA_Processes">工序集合</param>
        /// <param name="sn">SN</param>
        /// <param name="disidNo">工单号</param>
        public static void InsertProcessRecord2(List<scada_process> sCADA_Processes, scada_product_info workInfo)
        {
            var smallVersion = sCADA_Processes.FirstOrDefault()?.small_version;
            using (var ctx = pgsql.GetClient())
            {
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {

                    List<scada_process_record> Process_Records = new List<scada_process_record>();
                    foreach (var item in sCADA_Processes)
                    {
                        var CreateUser_Str = Env.DefaultUserName?.Value;
                        var process_Record = new scada_process_record()
                        {
                            disque_code = item.disque_code,
                            disque_name = item.disque_name,
                            disque_qty = item.disque_qty,
                            jobstep = item.jobstep,
                            jobstep_code = item.jobstep_code,
                            matdesc = item.matdesc,
                            matid = item.matid,
                            mbom = item.mbom,
                            small_version = item.small_version,
                            techprocess = item.techprocess,

                            createuser_str = CreateUser_Str,
                            workhour_num = item.workhour_num,

                            createtime_dt = System.DateTime.Now,
                            torquetimes = item.torquetimes,
                            op_group = item.op_group,
                            equipmentcode = item.equipmentCode,

                            serial_number = workInfo.serial_number,
                            disidno = workInfo.disidno,
                            woid = workInfo.id

                        };

                       
                        var res = ormClient.SqlSugarClient.Insertable<scada_process_record>(process_Record)
                            .IgnoreColumns(ignoreNullColumn: true)
                            .IgnoreColumns(it => it == "id").ExecuteReturnEntity();
                      
                    }
                }
            }
        }


        /// <summary>
        /// 插入工步历史表
        /// </summary>
        /// <param name="sCADA_JobSteps">工步信息</param>
        public static void InsertorUpdateJobStepsRecord(List<scada_jobstep_record> sCADA_JobStep_Records)
        {
            //try
            //{
                using (var ctx = pgsql.GetClient())
                {
                    if (pgsql.DBClient is SmdcOrmClient ormClient)
                    {
                        foreach (var item in sCADA_JobStep_Records)
                        {
                            var jobstep = ormClient.SqlSugarClient.Queryable<scada_jobstep_record>().
                            Where(m => m.processid == item.processid && m.jobstep_id_int == item.jobstep_id_int).First();
                            //item.equipmentcode = equipmentcode;//设备编码
                            if (jobstep != null)
                            {

                                item.createtime_dt = DateTime.Now;
                                ormClient.SqlSugarClient.Updateable<scada_jobstep_record>(item).
                                    Where(it => it.id == jobstep.id).ExecuteCommand();
                            }
                            else
                            {
                                item.modifytime_dt= DateTime.Now;
                                ormClient.SqlSugarClient.Insertable<scada_jobstep_record>(item).
                                   IgnoreColumns(it => it == "id").ExecuteCommand();
                            }
                        }
                       
                    }
                }
            //}
            //catch (Exception ex)
            //{
            //    System.Diagnostics.Trace.WriteLine($"保存工序出错:{ex.Message}");
            //}
        }

        public static void InsertorUpdateJobStepsRecord(scada_jobstep_record sCADA_JobStep_Records)
        {
            //try
            //{
                using (var ctx = pgsql.GetClient())
                {
                    if (pgsql.DBClient is SmdcOrmClient ormClient)
                    {
                        //var jobstep = ormClient.SqlSugarClient.Queryable<scada_jobstep_record>().
                        //Where(m => m.id == sCADA_JobStep_Records.id ).First();

                        //if (jobstep != null)
                        //{
                        //    ormClient.SqlSugarClient.Updateable<scada_jobstep_record>(sCADA_JobStep_Records).
                        //        Where(it => it.id == jobstep.id).ExecuteCommand();
                        //}
                        //else
                        //{                            
                        //    ormClient.SqlSugarClient.Insertable<scada_jobstep_record>(sCADA_JobStep_Records).
                        //       IgnoreColumns(it => it == "id").ExecuteCommand();
                        //}
                        ormClient.SqlSugarClient.Insertable<scada_jobstep_record>(sCADA_JobStep_Records).
                                   IgnoreColumns(it => it == "id").ExecuteCommand();



                     }
                }
            //}
            //catch (Exception ex)
            //{
            //    System.Diagnostics.Trace.WriteLine($"保存工序出错:{ex.Message}");
            //}
        }

        /// <summary>
        /// 数据记录提交到物料记录表
        /// </summary>
        /// <param name="material_Record"></param>
        public static void InsertMaterialRecord(scada_material_record material_Record)
        {
            //try
            //{
                using (var ctx = pgsql.GetClient())
                {
                    if (pgsql.DBClient is SmdcOrmClient ormClient)
                    {
                        ormClient.SqlSugarClient.Insertable<scada_material_record>(material_Record).IgnoreColumns(it => it == "id").ExecuteCommand();
                    }
                }
            //}
            //catch (Exception ex)
            //{
            //    System.Diagnostics.Debug.WriteLine($"插入到物料表失败{ex.Message}");
            //    throw;
            //}
        }

        /// <summary>


        #region 保存工艺路线的工序与工步
        public static int SaveProcess(scada_process sCADA_Process)
        {
            sCADA_Process.createuser_str = Env.DefaultUserName.Value;
            try
            {
                using (var ctx = pgsql.GetClient())
                {
                    if (pgsql.DBClient is SmdcOrmClient ormClient)
                    {
                       var id = ormClient.SqlSugarClient.Insertable<scada_process>(sCADA_Process).IgnoreColumns(it => it == "id").ExecuteReturnIdentity();
                        return id;
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine($"保存工序出错:{ex.Message}");
                return -1;
            }
            return -1;
        }

        public static void SaveStep(scada_jobstep Scada_step)
        {
            try
            {
                Scada_step.createuser_str = Env.DefaultUserName.Value;
                Scada_step.element_qty_int = Scada_step.element_qty_int == null ? 0 : Scada_step.element_qty_int;
                using (var ctx = pgsql.GetClient())
                {
                    if (pgsql.DBClient is SmdcOrmClient ormClient)
                    {
                        ormClient.SqlSugarClient.Insertable<scada_jobstep>(Scada_step).IgnoreColumns(it => it == "id").ExecuteCommand();
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine($"保存工序出错:{ex.Message}");
            }
        }
        #endregion

        #region 导入数据到临时表
        public static void InsertSmcDisid(List<smdc_disid_t> models)
        {
            using (var ctx = pgsql.GetClient())
            {
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    ormClient.SqlSugarClient.Insertable<smdc_disid_t>(models).IgnoreColumns(it => it == "id").ExecuteCommand();
                }
            }
        }

        public static void InsertSmcDisid_Ipt(List<smdc_disid_ipt> models)
        {
            using (var ctx = pgsql.GetClient())
            {
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    ormClient.SqlSugarClient.Insertable<smdc_disid_ipt>(models).IgnoreColumns(it => it == "id").ExecuteCommand();
                }
            }
        }


        public static int  InsertProductInfo(scada_product_info models)
        {
            using (var ctx = pgsql.GetClient())
            {
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    return ormClient.SqlSugarClient.Insertable<scada_product_info>(models).IgnoreColumns(it => it == "id").ExecuteReturnIdentity();
                }
            }
            return 0;
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="models"></param>
        public static void InsertMatCfg(List<scada_matcfg> models)
        {
            using (var ctx = pgsql.GetClient())
            {
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    ormClient.SqlSugarClient.Insertable<scada_matcfg>(models).IgnoreColumns(it => it == "id").ExecuteCommand();
                }
            }
        }

        public static void Insert_TorqueRecord(scada_torquerecord scada_Torquerecord)
        {
            //try
            //{
                using (var ctx = pgsql.GetClient())
                {
                    if (pgsql.DBClient is SmdcOrmClient ormClient)
                    {
                        ormClient.SqlSugarClient.Insertable<scada_torquerecord>(scada_Torquerecord).IgnoreColumns(it => it == "id").ExecuteCommand();
                    }
                }
            //}
            //catch (Exception ex)
            //{
            //    System.Diagnostics.Trace.WriteLine($"保存拧紧枪记录出错:{ex.Message}");
            //}
        }

        public static void Insert_TorqueRecord2(scada_torquerecord model)
        {
            //try
            //{
            using (var ctx = pgsql.GetClient())
            {
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    var item = ormClient.SqlSugarClient.Queryable<scada_torquerecord>().
                            Where(it => it.tighteningid == model.tighteningid && it.equipmentcode == model.equipmentcode).First();
                    if (item==null) {
                        ormClient.SqlSugarClient.Insertable<scada_torquerecord>(model).IgnoreColumns(it => it == "id").ExecuteCommand();
                    }
                    
                }
            }
            //}
            //catch (Exception ex)
            //{
            //    System.Diagnostics.Trace.WriteLine($"保存拧紧枪记录出错:{ex.Message}");
            //}
        }



        /// <summary>
        /// KJF 20210324
        /// 删除JobSteps根据ProcessRecordID
        /// </summary>
        public static void DeleteJobStepsByProcessRecordID(int ProcessRecordID)
        {
            if (pgsql.DBClient is SmdcOrmClient ormClient)
            {
                ormClient.SqlSugarClient.Deleteable<scada_jobstep_record>().
                    Where(m => m.processid == ProcessRecordID).ExecuteCommand();
            }
        }

        /// <summary>
        /// KJF 20210324
        /// 删除JobSteps根据ID
        /// </summary>
        public static void DeleteJobStepsByID(int Id)
        {
            if (pgsql.DBClient is SmdcOrmClient ormClient)
            {
                ormClient.SqlSugarClient.Deleteable<scada_jobstep_record>().
                    Where(m => m.id == Id).ExecuteCommand();
            }
        }


        /// <summary>
        /// KJF 20210330 新增
        /// 更新工单状态为"已报工"
        /// </summary>
        public static void UpdateWorkInfoToFinish(List<smdc_disid_t> WorkInfoList)
        {
            //try
            //{
                using (var ctx = pgsql.GetClient())
                {
                    if (pgsql.DBClient is SmdcOrmClient ormClient)
                    {
                        foreach (var item in WorkInfoList) {
                            item.state_int = (int)State.UploadFinish;
                            ormClient.SqlSugarClient.Updateable<smdc_disid_t>(item).IgnoreColumns(ignoreAllNullColumns: true).
                                        Where(it => it.id == item.id).ExecuteCommand();
                        }   
                    }
                }
            //}
            //catch (Exception ex)
            //{
            //    System.Diagnostics.Trace.WriteLine($"更新工单状态为已报工,出错:{ex.Message}");
            //}
        }



        /// <summary>
        /// KJF 20210330 新增
        /// 更新工单状态为"已报工"
        /// </summary>
        public static void UpdateWoInfoToFinish(smdc_disid_t WorkInfo)
        {
            //try
            //{
                using (var ctx = pgsql.GetClient())
                {
                    if (pgsql.DBClient is SmdcOrmClient ormClient)
                    {
                        if (WorkInfo != null)
                        {
                            WorkInfo.state_int = (int)State.UploadFinish;
                            ormClient.SqlSugarClient.Updateable<smdc_disid_t>(WorkInfo).IgnoreColumns(ignoreAllNullColumns: true).
                                        Where(it => it.id == WorkInfo.id).ExecuteCommand();
                        }
                    }
                }
            //}
            //catch (Exception ex)
            //{
            //    System.Diagnostics.Trace.WriteLine($"更新工单状态为已报工,出错:{ex.Message}");
            //}
        }


        /// <summary>
        /// KJF 20210330 新增
        /// 更新工单状态为"已报工"
        /// </summary>
        public static void UpdateWoInfoToFinishByWorkNoAndSN(string disid,string sn)
        {
            //try
            //{
                using (var ctx = pgsql.GetClient())
                {
                    if (pgsql.DBClient is SmdcOrmClient ormClient)
                    {
                        var item = ormClient.SqlSugarClient.Queryable<smdc_disid_t>().
                            Where(it => it.disidno == disid && it.serial_number == sn).First();
                        if (item != null)
                        {
                            item.state_int = (int)State.UploadFinish;
                            ormClient.SqlSugarClient.Updateable<smdc_disid_t>(item).IgnoreColumns(ignoreAllNullColumns: true).
                                            Where(m => m.id == item.id).ExecuteCommand();
                        }
                        
                    }
                }
            //}
            //catch (Exception ex)
            //{
            //    System.Diagnostics.Trace.WriteLine($"更新工单状态为已报工,出错:{ex.Message}");
            //}
        }

        /// <summary>
        /// KJF 20210720 新增
        /// 更新工件状态为"已上报"
        /// </summary>
        public static void UpdateProductInfoToFinish(int id)
        {
            //try
            //{
            using (var ctx = pgsql.GetClient())
            {
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    var item = ormClient.SqlSugarClient.Queryable<scada_product_info>().
                        Where(it => it.id == id ).First();
                    if (item != null)
                    {
                        item.state_int = (int)State.UploadFinish;
                        ormClient.SqlSugarClient.Updateable<scada_product_info>(item).IgnoreColumns(ignoreAllNullColumns: true).
                                        Where(m => m.id == item.id).ExecuteCommand();
                    }

                }
            }
            //}
            //catch (Exception ex)
            //{
            //    System.Diagnostics.Trace.WriteLine($"更新工单状态为已报工,出错:{ex.Message}");
            //}
        }


        /// <summary>
        /// 获取子工序
        /// </summary>
        /// <param name="sn"></param>
        public static scada_process_record  GetProcessRecord(int? id)
        {
            using (var ctx = pgsql.GetClient())
            {
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    var item=  ormClient.SqlSugarClient.Queryable<scada_process_record>().
                        Where(m => m.id==id).First();
                    return item;
                }
            }
            return null;
        }

        /// <summary>
        /// 获取工单
        /// </summary>
        /// <param name="sn"></param>
        public static smdc_disid_t GetWorkInfo(int? id)
        {
            using (var ctx = pgsql.GetClient())
            {
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    var item = ormClient.SqlSugarClient.Queryable<smdc_disid_t>().
                        Where(m => m.id == id).First();
                    return item;
                }
            }
            return null;
        }

        /// <summary>
        /// 获取工单
        /// </summary>
        /// <param name="sn"></param>
        public static scada_product_info GetProdInfo(int? id)
        {
            using (var ctx = pgsql.GetClient())
            {
                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    var item = ormClient.SqlSugarClient.Queryable<scada_product_info>().
                        Where(m => m.id == id).First();
                    return item;
                }
            }
            return null;
        }

        /// <summary>
        /// KJF 20210404 新增
        /// 查询设备信息
        /// </summary>
        /// <param name="sn"></param>
        /// <param name="disidNo"></param>
        /// <param name="state"></param>
        public static scada_equipment Query_scada_equipment(string equipmentid)
        {
            using (var ctx = pgsql.GetClient())
            {

                if (pgsql.DBClient is SmdcOrmClient ormClient)
                {
                    var equipment = ormClient.SqlSugarClient.Queryable<scada_equipment>().
                        Where(m => m.equipmentid == equipmentid).First();
                    return equipment;
                }
            }
            return null;

        }
    }
}
